<?php

namespace app\queue\redis\order;

use app\services\RequestService;
use plugin\admin\app\model\Good;
use plugin\admin\app\model\IgnoreGood;
use plugin\admin\app\model\NetworkOrder;
use plugin\admin\app\model\Order;
use plugin\admin\app\model\PriceLog;
use plugin\admin\app\model\RechargeLog;
use plugin\admin\app\model\RequestLog;
use plugin\admin\app\model\User;
use plugin\admin\app\model\YbUser;
use support\Cache;
use support\Db;
use support\Log;
use Webman\RedisQueue\Consumer;

class OrderNotify implements Consumer
{
    // 要消费的队列名
    public $queue = 'car-order-notify';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费
    public function consume($data)
    {
        Db::beginTransaction();
        try {
            $order = Order::where('sn', $data['sn'])->first();
            if (empty($order)){
                return true;
            }
//            // 查询下单商品
            $goods = Good::where('id',$order->goods_id)->first();
            if (empty($goods)){
                return true;
            }
//            $user = User::where('id',$data['user_id'])->first();
//            // 生成订单号
//            $sn = Order::getSn();
            $rake_price = $order->reward_total_price * ($goods->proportion / 100);
//            Order::create([
//                'sn'=>$sn,
//                'user_id'=>$data['user_id'],
//                'mobile' => $user->mobile,
//                'goods_id' => $goods->id,
//                'rake_price' => $rake_price,
//                'reward_total_price' => $goods->price,
//                'order_create_time' => $data['time'],
//                'pay_method' => $data['pay_method'],
//                'sesame_fraction' => $user->sesame_fraction??750,
//            ]);
            $reward_order = Order::where(['is_reward' => 0,'is_pay'=>1])->whereNotNull('reward_total_price')->orderBy('id')->first();
            $price_log = PriceLog::where('goods_id', $goods->id)->first();
            if ($order->is_record == 0){
                if (empty($price_log)){
                    $price_log = PriceLog::firstOrCreate([
                        'goods_id' => $goods->id,
                        'price' => $rake_price,
                    ],[
                        'goods_id' => $goods->id,
                        'price' => $rake_price,
                    ]);
                }else{
                    $price_log->price += $rake_price;
                    $price_log->save();
                }
            }

            $order->is_record = 1;
            $order->save();
            if ($price_log->price >= $reward_order->reward_total_price) {
                $user = User::where('id', $reward_order->user_id)->first();
                $user->money += $reward_order->reward_total_price;
                $user->save();
                $reward_order->is_reward = 1;
                $reward_order->reward_create_time = date('Y-m-d H:i:s');
                $reward_order->save();
                $price_log = PriceLog::where('goods_id', $goods->id)->first();
                $price_log->price -= $reward_order->reward_total_price;
                $price_log->save();
            }
            Db::commit();
        }catch (\Throwable $e){
            Db::rollBack();
            throw new \Exception($e->getMessage());
        }

    }

    public function onConsumeFailure(\Throwable $e, $package)
    {
        Log::error('消费失败：'.$e->getMessage());
    }
}